#Negative and Silencing Rates
#Alan Yan
#4-10-2020

#clear environment
rm(list = ls())

#load packages
library(pacman)
p_load(tidyverse,
       knitr,
       magick,
       webshot,
       kableExtra,
       gridExtra)

#load data
silencing <- read_csv("03-Silencing-Study/data/01-clean-data/clean_data.csv")

#creating a table of number of texts sent by texter 
texts_sent <- subset(silencing, silencing$male.instrument == 1 |
         silencing$female.instrument == 1 |
         silencing$no.name.instrument == 1 |
         silencing$gen.neutral.instrument == 1) %>%
  group_by(texter.id) %>%
  tally() 

texts_sent.1 <- subset(silencing[silencing$experiment1 == 1,], 
                       silencing$male.instrument[silencing$experiment1 == 1] == 1 |
                       silencing$female.instrument[silencing$experiment1 == 1] == 1 |
                       silencing$no.name.instrument[silencing$experiment1 == 1] == 1 |
                       silencing$gen.neutral.instrument[silencing$experiment1 == 1] == 1) %>%
  group_by(texter.id) %>%
  tally() 

texts_sent.2 <- subset(silencing[silencing$experiment1 == 0,], 
                       silencing$male.instrument[silencing$experiment1 == 0] == 1 |
                       silencing$female.instrument[silencing$experiment1 == 0] == 1 |
                       silencing$no.name.instrument[silencing$experiment1 == 0] == 1 |
                       silencing$gen.neutral.instrument[silencing$experiment1 == 0] == 1) %>%
  group_by(texter.id) %>%
  tally() 

#calculate the mean number of texts
texts.avg <- mean(texts_sent$n) %>%
  round(0)
texts.avg.1 <- mean(texts_sent.1$n) %>%
  round(0)
texts.avg.2 <- mean(texts_sent.2$n) %>%
  round(0)
  
#plot it as a historgram (Figure S1)
texts_sent.plot.main <- ggplot(texts_sent, aes(n)) +
  geom_histogram() +
  ggtitle("Both Studies") +
  xlab("Number of Texts Sent") +
  ylab("Number of Texters") +
  theme_minimal()+
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.x = element_blank(),
        axis.title.y = element_blank()) +
  geom_vline(xintercept = texts.avg, color = "red") +
  ylim(0, 20) 

texts_sent.plot.1 <- ggplot(texts_sent.1, aes(n)) +
  geom_histogram() +
  ggtitle("Study 1") +
  xlab("Number of Texts Sent") +
  ylab("Number of Texters") +
  theme_minimal()+
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.x = element_blank(),
        axis.title.y = element_blank()) +
  geom_vline(xintercept = texts.avg.1, color = "red") +
  ylim(0, 20)

texts_sent.plot.2 <- ggplot(texts_sent.2, aes(n)) +
  geom_histogram() +
  ggtitle("Study 2") +
  xlab("Number of Texts Sent") +
  ylab("Number of Texters") +
  theme_minimal()+
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.x = element_blank(),
        axis.title.y = element_blank()) +
  geom_vline(xintercept = texts.avg.2, color = "red") +
  ylim(0, 20)

texts_sent.plot <- grid.arrange(
  texts_sent.plot.main,
  texts_sent.plot.1,
  texts_sent.plot.2,
  nrow = 1,
  bottom = "Number of Texts Sent",
  left = "Number of Texters"
)
texts_sent.plot

#save plot
ggsave("07-Miscellaneous-Tables/02-tables/texts_sent.jpg", texts_sent.plot, height = 2.5, width = 7.5)
